﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.ObjectBuilder;
using GC2.Entities.Seguridad;
using GC2.Entities.Admin;
using GC2.Bussines.BMAdmin;

public partial class Admin_DetalleIngreso : GenericPage
{
    #region Propiedades
    Usuarios usuario = new Usuarios();
    private Decimal dEfectivo
    {
        get
        {
            return (Decimal)Session["dEfectivo"];
        }
        set
        {
            Session["dEfectivo"] = value;
        }
    }

    private Decimal dValores
    {
        get
        {
            return (Decimal)Session["dValores"];
        }
        set
        {
            Session["dValores"] = value;
        }
    }

    Cajas caja = new Cajas();
    int iCantidad = 0;
    Decimal dMonto = 0;

    #endregion

    #region Controles

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SetearCultura();
            SetearConexion();
            if (!IsPostBack)
            {
                usuario = (Usuarios)Session["UsuarioActual"];
                btnGuardar.Enabled = false;
                int iAccion = 1;
                if (Session["Accion"] != null)
                {
                    iAccion = Convert.ToInt16(Session["Accion"]);
                }

                if (iAccion == 1)
                {
                    Ingresos ingreso = new Ingresos();
                    ingreso = (Ingresos)Session["Ingreso"];
                    PoneCeros();
                    lblCaja.Text = ingreso.Id_Caja.ToString();
                    lblFecha.Text = ingreso.Fecha.ToShortDateString();
                    lblIngreso.Text = ingreso.Id_Ingreso.ToString();
                    decimal dCaja = Convert.ToDecimal(ingreso.Monto);
                    txtTCaja.Text = dCaja.ToString();
                    lblAccion.Text = "Carga de Detalle de Ingreso";
                    dEfectivo = 0;
                    dValores = 0;
                }
                else
                {
                    DetalleIngreso detalle = new DetalleIngreso();
                    detalle = (DetalleIngreso)Session["DetalleIngreso"];
                    lblCaja.Text = detalle.Id_Caja.ToString();
                    lblFecha.Text = detalle.Fecha.ToShortDateString();
                    lblIngreso.Text = detalle.Id_Ingreso.ToString();
                    decimal dCaja = Convert.ToDecimal(detalle.Total);
                    txtTCaja.Text = dCaja.ToString();
                    Hidrata(detalle);
                    txtObs.Text = detalle.Obs;
                    lblAccion.Text = "Viendo Detalle de Ingreso";
                    btnCalcular.Enabled = false;
                }
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion 

    #region btnGuardar_Click
    protected void btnGuardar_Click(object sender, EventArgs e)
    {
        try
        {
            Decimal dDiferencia = Convert.ToDecimal(txtDiferencia.Text.Trim());
            if (dDiferencia == 0)
            {
                DetalleIngreso detalle = new DetalleIngreso();
                detalle = Deshidratar();
                BMDetalleIngreso bmDetalleIngreso = new BMDetalleIngreso(Connection);
                bmDetalleIngreso.BMDetalleIngresoInsert(detalle);
                btnGuardar.Enabled = false;
                btnCalcular.Enabled = false;
                lblAccion.Text = "Detalle Guardado";
            }
            else
            {
                mpeDiferencia.Show();
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }


    #region btnCancelar_Click
    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("~/Seguridad/Default.aspx", false);
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #endregion

    #region btnCalcular_Click
    protected void btnCalcular_Click(object sender, EventArgs e)
    {
        try
        {
            Recalcula();
            btnGuardar.Enabled = true;
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #endregion

    #region Métodos

    #region Recalcula
    protected void Recalcula()
    {
        try
        {
            dValores = 0;
            dEfectivo = 0;
            decimal dDiferencia = 0;
            

            if (txtCtv5.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtCtv5.Text.Trim());
                dMonto = Convert.ToDecimal(0.05 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtCtv10.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtCtv10.Text.Trim());
                dMonto = Convert.ToDecimal(0.10 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtCtv25.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtCtv25.Text.Trim());
                dMonto = Convert.ToDecimal(0.25 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtCtv50.Text != "0")
            {
                iCantidad = Convert.ToInt16(txtCtv50.Text.Trim());
                dMonto = Convert.ToDecimal(0.50 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtCtv100.Text != "0")
            {
                dMonto = Convert.ToInt16(txtCtv100.Text.Trim());
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txt2.Text != "0")
            {
                iCantidad = Convert.ToInt16(txt2.Text.Trim());
                dMonto = Convert.ToDecimal(2 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txt5.Text != "0")
            {
                iCantidad = Convert.ToInt16(txt5.Text.Trim());
                dMonto = Convert.ToDecimal(5 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txt10.Text != "0")
            {
                iCantidad = Convert.ToInt16(txt10.Text.Trim());
                dMonto = Convert.ToDecimal(10 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txt20.Text != "0")
            {
                iCantidad = Convert.ToInt16(txt20.Text.Trim());
                dMonto = Convert.ToDecimal(20 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txt50.Text != "0")
            {
                iCantidad = Convert.ToInt16(txt50.Text.Trim());
                dMonto = Convert.ToDecimal(50 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txt100.Text != "0")
            {
                iCantidad = Convert.ToInt16(txt100.Text.Trim());
                dMonto = Convert.ToDecimal(100 * iCantidad);
                dValores += dMonto;
                dEfectivo += dMonto;
            }
            if (txtTCheques.Text != "0")
            {
                dMonto = Convert.ToDecimal(txtTCheques.Text.Trim());
                dValores += dMonto;
            }

            decimal decTemp = Convert.ToDecimal(dValores);
            decTemp = Math.Round(decTemp, 2);
            txtTValores.Text = decTemp.ToString();
            decimal dCaja = Convert.ToDecimal(txtTCaja.Text.Trim());
            dDiferencia = decTemp - dCaja;
            txtDiferencia.Text = dDiferencia.ToString();
            decTemp = Convert.ToDecimal(dEfectivo);
            decTemp = Math.Round(decTemp, 2);
            txtTEfectivo.Text = decTemp.ToString();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region Deshidratar
    protected DetalleIngreso Deshidratar()
    {
        DetalleIngreso detalle = new DetalleIngreso();
        try
        {

            if (txtCtv5.Text != "0")
            {
                detalle.Ctv5 = Convert.ToInt16(txtCtv5.Text.Trim());
            }
            if (txtCtv10.Text != "0")
            {
                detalle.Ctv10 = Convert.ToInt16(txtCtv10.Text.Trim());
            }
            if (txtCtv25.Text != "0")
            {
                detalle.Ctv25 = Convert.ToInt16(txtCtv25.Text.Trim());
            }
            if (txtCtv50.Text != "0")
            {
                detalle.Ctv50 = Convert.ToInt16(txtCtv50.Text.Trim());
            }
            if (txtCtv100.Text != "0")
            {
                detalle.Ctv100 = Convert.ToInt16(txtCtv100.Text.Trim());
            }
            if (txt2.Text != "0")
            {
                detalle.Billete2 = Convert.ToInt16(txt2.Text.Trim());
            }
            if (txt5.Text != "0")
            {
                detalle.Billete5 = Convert.ToInt16(txt5.Text.Trim());
            }
            if (txt10.Text != "0")
            {
                detalle.Billete10 = Convert.ToInt16(txt10.Text.Trim());
            }
            if (txt20.Text != "0")
            {
                detalle.Billete20 = Convert.ToInt16(txt20.Text.Trim());
            }
            if (txt50.Text != "0")
            {
                caja.Billete50 = Convert.ToInt16(txt50.Text.Trim());
            }
            if (txt100.Text != "0")
            {
                detalle.Billete100 = Convert.ToInt16(txt100.Text.Trim());
            }
            if (txtTCheques.Text != "0")
            {
                detalle.Cheques = Convert.ToInt16(txtTCheques.Text.Trim());
            }
            detalle.Efectivo = Convert.ToDecimal(txtTEfectivo.Text.Trim());

            detalle.Fecha = DateTime.Now.Date;
            detalle.Id_Caja = Convert.ToInt16(lblCaja.Text);

            Usuarios usuario = new Usuarios();
            usuario = (Usuarios)Session["UsuarioActual"];
            caja.Id_Usuario = usuario.Id_Usuario;

            detalle.Obs = txtObs.Text;

            // Claves
            Ingresos ingreso = (Ingresos)Session["Ingreso"];
            detalle.Id_Caja = ingreso.Id_Caja;
            detalle.Id_Ingreso = ingreso.Id_Ingreso;
            detalle.Total = ingreso.Monto;

        }
        catch (Exception ex) { ErrorUI(ex); }
        return detalle;
    }
    #endregion

    #region Hidrata
    protected void Hidrata(DetalleIngreso detalle)
    {
        try
        {
            
            txtCtv5.Text = detalle.Ctv5.ToString();

            txtCtv10.Text = detalle.Ctv10.ToString();
           
            txtCtv25.Text = detalle.Ctv25.ToString();
           
            txtCtv50.Text = detalle.Ctv50.ToString();
            
            txtCtv100.Text = detalle.Ctv100.ToString();

            txt2.Text = detalle.Billete2.ToString();
            
            txt5.Text = detalle.Billete5.ToString();
            
            txt10.Text = detalle.Billete10.ToString();
            
            txt20.Text = detalle.Billete20.ToString();
            
            txt50.Text = detalle.Billete50.ToString();
            
            txt100.Text = detalle.Billete100.ToString();
           
            txtTCheques.Text = detalle.Cheques.ToString();

            lblFecha.Text = detalle.Fecha.ToShortDateString();
            lblCaja.Text = detalle.Id_Caja.ToString();
            lblIngreso.Text = detalle.Id_Ingreso.ToString();

            Recalcula();
            
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region PoneCeros
    protected void PoneCeros()
    {
        try
        {
            txtCtv5.Text = "0";
            txtCtv10.Text = "0";
            txtCtv25.Text = "0";
            txtCtv50.Text = "0";
            txtCtv100.Text = "0";
            txt2.Text = "0";
            txt5.Text = "0";
            txt10.Text = "0";
            txt20.Text = "0";
            txt50.Text = "0";
            txt100.Text = "0";
            txtTCheques.Text = "0";
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #endregion
}